package com.xfcycle.zebrabikegp;

import android.content.Context;
import android.os.Environment;
import android.os.SystemClock;
import android.support.test.runner.AndroidJUnit4;
import android.test.mock.MockContext;
import android.util.Log;

import com.xfcycle.zebrabikegp.listener.BleInstructQueueLooperListener;
import com.xfcycle.zebrabikegp.util.BluetoothInstructStackUtil;
import com.xfcycle.zebrabikegp.util.DateFormatUtil;
import com.xfcycle.zebrabikegp.util.HandleDataByFileUtil;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;

/**
 * 相关测试
 * Created by xFcycle on 2017/5/11.
 */

@RunWith(AndroidJUnit4.class)
public class HandDataByFileUnitTest {

    @Test
    public void getDataFromFile() {
        Context context = new MockContext();
        String str = HandleDataByFileUtil.getDatafromProperties("device_filter_name", context);
    }

    /**
     * 测试蓝牙指令栈
     */
    @Test
    public void testBleStackUtil() {
        BluetoothInstructStackUtil bluetoothInstructStackUtil = BluetoothInstructStackUtil.getInstance();
        BluetoothInstructStackUtil.addInstructToQueue("000000");
        bluetoothInstructStackUtil.setInstructQueueLooperListener(new BleInstructQueueLooperListener() {
            @Override
            public void handleCurrentBleInstruct(String instruct) {
                Log.e("test", "handleCurrentBleInstruct: ++++++++++" + instruct + "+++++++" + DateFormatUtil.Long2StandardTime(System.currentTimeMillis()));
            }
        });

        SystemClock.sleep(1000);
//        BluetoothInstructStackUtil.stopInstructLooper();

        BluetoothInstructStackUtil.addInstructToQueue("111111");

        SystemClock.sleep(300);

        BluetoothInstructStackUtil.addInstructToQueue("222222");
        BluetoothInstructStackUtil.addInstructToQueue("444444");

        SystemClock.sleep(700);

        BluetoothInstructStackUtil.addInstructToQueue("333333");
    }

    /**
     * 测试获取应用缓存数据
     */
    @Test
    public void testGetAppCache() {
        Log.e("test", "testGetAppCache: " + "/data/user/0/com.xfcycle.zebrabikegp/cache");
        Log.e("test", "testGetAppCache: " + "/data/data/com.xfcycle.zebrabikegp/cache");

        File file1 = new File("/data/user/0/com.xfcycle.zebrabikegp/cache");
        File file = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            file = new File("/storage/emulated/0/Android/data/com.xfcycle.zebrabikegp/cache");
            Log.e("test", "testGetAppCache: ------------" + file);
        }

        cleanCache(file);
        cleanCache(file1);
        Log.e("test", "testGetAppCache: ------------" + ((fileSize(file) + fileSize(file1))));
    }

    private long fileSize(File file) {

        if (null == file) {
            return 0;
        }

        long size = 0;

        if (file.exists() && file.isDirectory()) {
            String[] childrenFileNames = file.list();
            for (int i = 0; i < childrenFileNames.length; i++) {
                File childrenFile = new File(file, childrenFileNames[i]);
                if (childrenFile.exists() && childrenFile.isFile()) {
                    size += childrenFile.length();
                }
                if (childrenFile.exists() && childrenFile.isDirectory()) {
                    size += fileSize(childrenFile);
                }
            }
        }

        return size;
    }

    private void cleanCache(File file) {
        if (null == file) {
            return;
        }

        if (file.exists() && file.isDirectory()) {
            String[] childrenFileNames = file.list();
            for (int i = 0; i < childrenFileNames.length; i++) {
                File childrenFile = new File(file, childrenFileNames[i]);
                if (childrenFile.exists() && childrenFile.isFile()) {
                    childrenFile.delete();
                }
                if (childrenFile.exists() && childrenFile.isDirectory()) {
                    cleanCache(childrenFile);
                }
            }
        }
    }
}
